package com.core.website.api;

import com.core.website.common.Result;
import com.core.website.common.ResultUtil;
import com.core.website.entity.Column;
import com.core.website.entity.User;
import com.core.website.mapper.RoleMapper;
import com.core.website.service.ColumnService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/columnApi")
public class ColumnApi {
    @Autowired
    private ColumnService columnService;
    @Autowired
    private RoleMapper roleMapper;
    @PostMapping("/add")
    public Result add(@RequestBody Column column){
        column.setColumnName(column.getName());
        int result=columnService.add(column);
        return ResultUtil.success(result);
    }
    @PostMapping("/edit")
    public Result edit(@RequestBody Column column){
        column.setColumnName(column.getName());
        int result=columnService.edit(column);
        return ResultUtil.success(result);
    }
    @PostMapping("/delete")
    public Result delete(@RequestBody List<String> ids){
        int result=columnService.delete(ids);
        return ResultUtil.success(result);
    }
    @PostMapping("/queryAll")
    public Result queryAll(@RequestBody Column column, HttpServletRequest request){
        List<Column> result=columnService.queryAll(column);
        return ResultUtil.success(result);
    }
    // 审核专门使用的接口
    @PostMapping("/queryAuthColumn")
    public Result queryAuthColumn(@RequestBody Column column, HttpServletRequest request){
        User subject = (User) SecurityUtils.getSubject().getPrincipal();
        if(null!=subject){
            Set<String> list = roleMapper.queryRoleCodeByUserId(subject.getId());
            if(list.contains("admin")){
                // 管理员查询所有栏目
                List<Column> result=columnService.queryAll(column);
                return ResultUtil.success(result);
            }else{
                //普通用户 查询所具有权限的栏目
                List<Column> result2 = columnService.queryByOrgId(subject.getOrganizationId());
                return ResultUtil.success(result2);
            }
        }

        return ResultUtil.error(-500,"数据有误");
    }

    @PostMapping("/queryTree")
    public Result queryTree(){
        List<Column> result=columnService.queryTree();
        return ResultUtil.success(result);
    }

    @GetMapping("/queryOne")
    public Result queryOne(String id){
        Column result=columnService.queryOne(id);
        return ResultUtil.success(result);
    }
}
